package com.sx.micowell.module.trainplan

import android.annotation.SuppressLint
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.ToastUtils
import com.gyf.immersionbar.ImmersionBar
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import com.haibin.calendarview.CalendarView.OnCalendarInterceptListener
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.ActRangePlanBinding
import com.sx.micowell.framework.app.MyActivity
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.model.TrainPlanModel

/**
 * @author: YY
 * @time: 2021/8/5
 * @describe 选择日期范围
 */
@Route(path = RoutePath.SELECT_DATE)
class SelectDateActivity : MyActivity<ActRangePlanBinding>(), CalendarView.OnCalendarRangeSelectListener,
                                                              CalendarView.OnMonthChangeListener,
                                                              CalendarView.OnCalendarInterceptListener {

    lateinit var viewModel: TrainPlanModel


    override fun initViewModel() {
        viewModel = getActivityScopeViewModel(TrainPlanModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.act_range_plan, BR.viewModel, viewModel)
    }

    override fun initView() {
        ImmersionBar.with(this).statusBarDarkFont(true)
            .fitsSystemWindows(true).init()

        binding.calendarView.setSelectRange(7, -1)
        binding.calendarView.setOnCalendarRangeSelectListener(this)
        binding.calendarView.setOnMonthChangeListener(this)
        //设置日期拦截事件，当前有效
        //设置日期拦截事件，当前有效
        binding.calendarView.setOnCalendarInterceptListener(this)
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        binding.tvYear.text = binding.calendarView.curYear.toString() + "年" +
                binding.calendarView.curMonth.toString() + "月"
    }

    override fun onCalendarSelectOutOfRange(calendar: Calendar?) {

    }

    override fun onSelectOutOfRange(calendar: Calendar?, isOutOfMinRange: Boolean) {
        ToastUtils.showShort("请选择7天或以上天数")
    }

    @SuppressLint("SetTextI18n")
    override fun onCalendarRangeSelect(calendar: Calendar?, isEnd: Boolean) {
        if (!isEnd) {
            binding.tvLeftDate.text = calendar!!.month.toString() + "月" + calendar.day + "日"
            binding.tvRightDate.text = ""

            viewModel.selectStartTime = calendar.year.toString() + "/" +
                    calendar.month.toString() + "/" + calendar.day

            viewModel.selectEndTime = ""
        } else {
            binding.tvRightDate.text = calendar!!.month.toString() + "月" + calendar.day + "日"

            viewModel.selectEndTime = calendar.year.toString() + "/" +
                    calendar.month.toString() + "/" + calendar.day
        }

    }

    @SuppressLint("SetTextI18n")
    override fun onMonthChange(year: Int, month: Int) {
        binding.tvYear.text = year.toString() + "年" + month.toString() + "月"
    }

    override fun onCalendarIntercept(calendar: Calendar?): Boolean {
        return calendar?.timeInMillis!! < getCurrentDayMill()
    }

    override fun onCalendarInterceptClick(calendar: Calendar?, isClick: Boolean) {

    }


    private fun getCurrentDayMill(): Long {
        val calendar = java.util.Calendar.getInstance()
        calendar[java.util.Calendar.HOUR] = 0
        calendar[java.util.Calendar.MINUTE] = 0
        calendar[java.util.Calendar.MILLISECOND] = 0
        return calendar.timeInMillis
    }
}